package com.bank.Servlets;

import com.bank.dao.impl.InsertDaoImpl;
import com.bank.dao.impl.QueryDaoImpl;
import com.bank.dao.impl.UpDateDaoImpl;
import com.bank.model.BankCard;
import com.bank.model.Transaction;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/TurnMoney")
public class TurnMoneyServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        String bank_id1 = request.getParameter("bank_id1");
        String bank_id2 = request.getParameter("bank_id2");
        double money = Double.parseDouble(request.getParameter("money"));


        QueryDaoImpl queryDao=new QueryDaoImpl();
        double count1 = queryDao.queryTb_Bankcard(bank_id1).getCount();
        double count2 = queryDao.queryTb_Bankcard(bank_id2).getCount();

        Date date=new Date();//获取当前交易时间
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        String newDate=formatter.format(date);

        //正式交易
        UpDateDaoImpl upDateDao=new UpDateDaoImpl();
        upDateDao.upDate(count1-money,bank_id1);
        upDateDao.upDate(count2+money,bank_id2);


        //交易完
        //设置交易明细
        Transaction transaction=new Transaction();
        transaction.setDatetime(newDate);
        transaction.setTs_money(money);
        transaction.setCard_id(queryDao.queryTb_Bankcard(bank_id1).getCard_id());
        InsertDaoImpl insertDao=new InsertDaoImpl();
        insertDao.addTransaction(transaction);


        HttpSession session = request.getSession();
        session.setAttribute("transaction",transaction);
        response.sendRedirect("turnmoney.jsp");

    }
}
